Skip to content

[pigeon] updates toString and isNullish methods#11625

Open
tarrinneal wants to merge 6 commits into
flutter:mainfrom
tarrinneal:toString
Open

[pigeon] updates toString and isNullish methods#11625
tarrinneal wants to merge 6 commits into
flutter:mainfrom
tarrinneal:toString

Conversation

@tarrinneal
Copy link
Copy Markdown
Contributor

prequel pr to #11352 to land non NI changes to simplify pr.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates Pigeon to version 26.4.0, introducing automatic generation of toString (or equivalent) methods for data classes across all supported languages. It also refines the Swift isNullish utility to handle nested optional values. Review feedback highlights a critical logic error in the C++ generator that results in unbalanced braces and corrupted code, along with a compilation issue when handling C++ enums. Additionally, for Swift, the feedback identifies a redundant protocol conformance error in subclasses and suggests using String(describing:) to prevent compiler warnings during string interpolation of optional fields.

Comment thread packages/pigeon/lib/src/cpp/cpp_generator.dart Outdated
Comment on lines +1161 to +1171
indent.writeln('ss << *$name;');
}
});
indent.nest(1, () {
indent.writeln('ss << "null";');
});
} else {
if (field.type.isClass) {
indent.writeln('ss << $name.ToString();');
} else {
indent.writeln('ss << $name;');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

C++ enum class types do not support the << operator for std::ostream by default. If a data class contains an enum field, the generated ToString method will fail to compile. You should check if the field is an enum and cast it to its underlying integer type before appending it to the stream.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gemini is correct here. You'll also need custom handling of maps, and maybe lists.

Comment thread packages/pigeon/lib/src/swift/swift_generator.dart Outdated
final Iterable<String> fieldStrings = classDefinition.fields.map((
NamedType field,
) {
return '${field.name}: \\(${field.name})';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In Swift, string interpolation of optional values triggers a compiler warning (e.g., "Expression implicitly coerced from 'String?' to 'Any'"). It is recommended to wrap optional fields in String(describing:) to silence this warning and provide a consistent string representation.

Suggested change
return '${field.name}: \\(${field.name})';
return '${field.name}: \(String(describing: ${field.name}))';

@github-actions github-actions Bot removed the CICD Run CI/CD label May 1, 2026
@stuartmorgan-g stuartmorgan-g added the CICD Run CI/CD label May 4, 2026
Copy link
Copy Markdown
Collaborator

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The native unit tests should have a test that calls the to-string method for a test object and ensure that the expected string results, so that we are validating that the methods do what we expect (and also have a clear snapshot of what the expected output actually is).

Comment on lines +1161 to +1171
indent.writeln('ss << *$name;');
}
});
indent.nest(1, () {
indent.writeln('ss << "null";');
});
} else {
if (field.type.isClass) {
indent.writeln('ss << $name.ToString();');
} else {
indent.writeln('ss << $name;');
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gemini is correct here. You'll also need custom handling of maps, and maybe lists.

Comment thread packages/pigeon/lib/src/cpp/cpp_generator.dart Outdated
Comment thread packages/pigeon/lib/src/dart/dart_generator.dart Outdated
Comment thread packages/pigeon/lib/src/gobject/gobject_generator.dart Outdated
NamedType field,
) {
if (_usesPrimitive(field.type)) {
return '@(self.${field.name})';
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW it would be conceptually cleaner to use the right format string for each primitive type, rather than boxing them just to print them as objects. But since this is just a nice-to-have utility method, and Obj-C isn't a development focus, it's fine to leave as-is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lmk if my change is what you had in mind.

Comment thread packages/pigeon/platform_tests/test_plugin/linux/pigeon/core_tests.gen.cc Outdated
@github-actions github-actions Bot removed the CICD Run CI/CD label May 7, 2026
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label May 11, 2026
@github-actions github-actions Bot removed the CICD Run CI/CD label May 12, 2026
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label May 13, 2026
@tarrinneal
Copy link
Copy Markdown
Contributor Author

/gemini review

@tarrinneal tarrinneal requested a review from stuartmorgan-g May 13, 2026 02:48
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements toString (or equivalent) method generation for data classes across all supported Pigeon generators, including Dart, Java, Kotlin, Swift, Objective-C, C++, and GObject. It also updates the Swift isNullish helper to correctly handle double-nested Any? values and adds new list-based echo methods to the core integration tests. Feedback identifies a missing nullability check for boolean fields in the GObject generator and potential integer truncation issues in the Objective-C description implementation on 32-bit platforms. Additionally, a suggestion was made to maintain field name consistency in GObject string representations.

Comment on lines +1444 to +1447
} else if (field.type.baseName == 'bool') {
indent.writeln(
'g_string_append(str, self->$fieldName ? "true" : "false");',
);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The bool case is missing nullability handling. In the GObject generator, nullable booleans are represented as gboolean*. Using self->fieldName directly in a ternary will check the pointer's validity rather than the boolean value, and it will print "false" instead of "null" when the pointer is null.

        } else if (field.type.baseName == 'bool') {
          if (field.type.isNullable) {
            indent.writeScoped('if (self->$fieldName != nullptr) {', '}', () {
              indent.writeln(
                'g_string_append(str, *self->$fieldName ? "true" : "false");',
              );
            });
            indent.writeScoped('else {', '}', () {
              indent.writeln('g_string_append(str, "null");');
            });
          } else {
            indent.writeln(
              'g_string_append(str, self->$fieldName ? "true" : "false");',
            );
          }
        }

case 'bool':
return '${field.name}: %@';
case 'int':
return '${field.name}: %ld';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Pigeon int fields are generated as int64_t in Objective-C. Using %ld with a (long) cast can lead to integer truncation on 32-bit platforms. It is safer to use %lld with a (long long) cast to ensure full 64-bit precision is maintained in the description.

Suggested change
return '${field.name}: %ld';
case 'int':
return '${field.name}: %lld';

case 'bool':
return 'self.${field.name} ? @"true" : @"false"';
case 'int':
return '(long)self.${field.name}';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

As noted above, int64_t should be cast to (long long) when used with the %lld format specifier to avoid truncation on 32-bit architectures.

Suggested change
return '(long)self.${field.name}';
case 'int':
return '(long long)self.${field.name}';

enumerate(classDefinition.fields, (int index, final NamedType field) {
final String fieldName = _getFieldName(field.name);
final comma = index == 0 ? '' : ', ';
indent.writeln('g_string_append(str, "$comma$fieldName: ");');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency with other generated platforms (Dart, Java, Kotlin, Swift, C++), the label in the string representation should use the original field name (field.name) rather than the snake_case GObject member name (fieldName).

Suggested change
indent.writeln('g_string_append(str, "$comma$fieldName: ");');
indent.writeln('g_string_append(str, "$comma${field.name}: ");');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants